package com.secsys.action.test;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.transaction.JOnASTransactionManagerLookup;

import com.secsys.action.BaseAction;
import com.secsys.dto.SearchDTO;
import com.secsys.model.TbSysUser;
import com.secsys.service.SearchService;
import com.secsys.service.UserService;
import com.secsys.util.Constant;
import com.secsys.util.MySqlUtil;

public class JsonTestAction extends BaseAction<SearchDTO>{
	
      private SearchDTO searchDTO;
      private SearchService searchService;  
      private UserService userService;
	public SearchDTO getSearchDTO() {
		return searchDTO;
	}

	public void setSearchDTO(SearchDTO searchDTO) {
		this.searchDTO = searchDTO;
	}

	@Override
	public SearchDTO getModel() {
		// TODO Auto-generated method stub
		searchDTO=new SearchDTO();
		
		return searchDTO;
	}
	/**
	 * 
	 * @Description Json 传输测试
	 * @return
	 * @throws Exception
	 */
	
    public String JsonTest()throws Exception{
    	       
            	PrintWriter out=ServletActionContext.getResponse().getWriter();
            	 

            		String hql = MySqlUtil.basicMatchHQL(searchDTO.getSearchType(),
            				Constant.TbBbsPost);
            		List searchList=null;
                     if(searchDTO.getKeyWord()!=null&&searchDTO.getSearchType().equals("author")){
                    	    TbSysUser user = userService.queryUserByName(searchDTO.getKeyWord());
                    	    searchList = searchService.queryPostByParam(hql, user);
                     }else{
                    	 searchList = searchService.queryPostByParam(hql, MySqlUtil
                 				.likeMatchParaten(searchDTO.getKeyWord()));
                     }
            	 JSONArray test=JSONArray.fromObject(searchList);
            	 System.out.println(test.toString());
            	      out.print(test);
    	
    	return null;
    }

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
